import { DatePicker, Input, InputNumber, message, Select } from 'antd';
import React from 'react';
import { Field } from '@/dictionary/index.d';
import Dictionary from '@/dictionary/index';
import PicturesWallUpload from '@/components/hyperspace/components/PicturesWallUpload';

/**
 * 获取Input
 *
 * @param field
 */
export function getFormInputByField(field: Field) {
  if (typeof field.dataType === 'object') {
    return field.dataType;
  }
  switch (field.dataType) {
    case 'string':
      return <Input style={{ width: '100%' }} />;
    case 'number':
      return <InputNumber style={{ width: '100%' }} />;
    case 'date':
      return <DatePicker style={{ width: '100%' }} />;
    case 'dateRange':
      return <DatePicker.RangePicker style={{ width: '100%' }} />;
    case 'textarea':
      return <Input.TextArea rows={4} style={{ width: '100%' }} />;
    case 'select':
      return (
        <Select style={{ width: '100%' }}>
          {// eslint-disable-next-line max-len
          field.options ? (
            field.options.map(item => <Select.Option value={item.key}>{item.value}</Select.Option>)
          ) : (
            <></>
          )}
        </Select>
      );
    case 'file':
      return <PicturesWallUpload />;
    default:
      return <Input />;
  }
}

let count = 0;

/**
 * 获取字段定义，默认获取字典定义，当本地定义后，以本地定义覆盖字典
 *
 * @param fields
 * @param localFieldDefines
 */
export function getFieldDefines(fields: string[], localFieldDefines: Field[]) {
  const fieldDefines: Field[] = Dictionary.getFieldsByNames(fields);

  let fieldDefinesTemp: Field[] = [];
  if (localFieldDefines && localFieldDefines.length > 0) {
    fieldDefinesTemp = fieldDefines.map(fieldDefine => {
      // eslint-disable-next-line no-restricted-syntax
      for (const localFieldDefine of localFieldDefines) {
        if (localFieldDefine.name === fieldDefine.name) {
          return localFieldDefine;
        }
      }
      return fieldDefine;
    });
  } else {
    fieldDefinesTemp = fieldDefines;
  }

  if (fields.length > fieldDefinesTemp.length) {
    const tipColumns = fields
      .filter(item => {
        // eslint-disable-next-line no-restricted-syntax
        for (const fieldDefine of fieldDefinesTemp) {
          if (fieldDefine.name === item) {
            return false;
          }
        }
        return true;
      })
      .filter(item => {
        // eslint-disable-next-line no-restricted-syntax
        for (const fieldDefine of localFieldDefines) {
          if (fieldDefine.name === item) {
            fieldDefinesTemp.push(fieldDefine);
            return false;
          }
        }
        return true;
      });

    if (tipColumns.length > 0 && count === 0) {
      count += 1;
      message.error(`找不到字典定义，字段：${JSON.stringify(tipColumns)}`, () => {
        count -= 1;
      });
    }
  }

  // eslint-disable-next-line max-len
  fieldDefinesTemp = fieldDefinesTemp.sort(
    (item1: Field, item2: Field) => fields.indexOf(item1.name) - fields.indexOf(item2.name),
  );

  return fieldDefinesTemp;
}
